﻿using Microsoft.AspNet.SignalR.Hosting;
using Microsoft.JSInterop.Infrastructure;
using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.EntityFramework;
using SmartWMS.Server.BasicsDatumServer.WarehouseManagementService.Dto;
using SmartWMS.Server.Repository;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartWMS.Server.BasicsDatumServer.WarehouseManagementService
{
	/// <summary>
	/// 仓库管理
	/// </summary>
	public class WarehouseManagementService : IWarehouseManagementService
	{
		private readonly IRepository<Warehouse> _warehouseResponse;
		private readonly SmartWMSContext Db;

		public WarehouseManagementService(IRepository<Warehouse> warehouseResponse, SmartWMSContext db)
		{
			_warehouseResponse = warehouseResponse;
			Db = db;
		}

		/// <summary>
		/// 测试
		/// </summary>
		/// <returns></returns>
		public async Task<Response<List<WarehouseDto>>> Get()
		{
			try
			{
				var list = await _warehouseResponse.GetAllListAsync();
				var result = list.MapToList<WarehouseDto>();
				return Response<List<WarehouseDto>>.Ok(result);
			}
			catch (Exception)
			{
				throw;
			}
		}

		/// <summary>
		/// 异步显示仓库管理
		/// </summary>
		/// <param name="dto">要查询的条件</param>
		/// <returns></returns>
		public async Task<(int pageCount, int totalCount, Response<List<WarehouseDto>> data)> GetWarehouseManagermentDtoAsync(SearchDto dto)
		{
			try
			{
				var list = await _warehouseResponse.GetAllListAsync();

				if (!string.IsNullOrEmpty(dto.WarehouseCode))
				{
					list = list.Where(x => x.WarehouseCode.Contains(dto.WarehouseCode)).ToList();
				}

				if (!string.IsNullOrEmpty(dto.Name))
				{
					list = list.Where(x => x.Name.Contains(dto.Name)).ToList();
				}

				if (!string.IsNullOrEmpty(dto.WarehouseType))
				{
					list = list.Where(x => x.WarehouseType == dto.WarehouseType).ToList();
				}

				//计算总页数
				int totalCount = list.Count();
				int pageCount = (int)Math.Ceiling(totalCount * 1.0 / dto.pageSize);
				list = list.Skip((dto.pageIndex - 1) * dto.pageSize).Take(dto.pageSize).ToList();

				var result = list.MapToList<WarehouseDto>();
				return (pageCount, totalCount, Response<List<WarehouseDto>>.Ok(result));
			}
			catch (Exception)
			{
				throw;
			}
		}


		/// <summary>
		/// 新增仓库
		/// </summary>
		/// <param name="info">要添加的仓库</param>
		/// <returns>返回受影响行数</returns>
		public async Task<Response<bool>> AddWarehouseManagerment(WarehouseDto info)
		{
			try
			{
				var data = info.MapTo<Warehouse>();
				//设置data中的其中一个字段设置一个随机数
				//data.WarehouseCode = new Random().Next(0000186, 999999).ToString();
				var result = await _warehouseResponse.InsertAsync(data);
				return result > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
			}
			catch (Exception)
			{
				throw;
			}
		}

		/// <summary>
		/// 反填仓库管理
		/// </summary>
		/// <param name="id">要反填的Id</param>
		/// <returns>返回首行首列</returns>
		public Response<WarehouseDto> FilWarehouseDto(int id)
		{
			try
			{
				var list = _warehouseResponse.GetListQuery().FirstOrDefault(x=>x.Id == id);
				var result = list.MapTo<WarehouseDto>();
				return Response<WarehouseDto>.Ok(result);
			}
			catch (Exception)
			{
				throw;
			}
		}

		/// <summary>
		/// 修改仓库
		/// </summary>
		/// <param name="info">要修改的仓库实体</param>
		/// <returns></returns>
		public async Task<Response<bool>> UpdWarehouseManagerment(WarehouseDto info)
		{
			try
			{
				var list = info.MapTo<Warehouse>();
				var result = await _warehouseResponse.UpdateAsync(list);
				return result > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
			}
			catch (Exception)
			{
				throw;
			}
		}

		/// <summary>
		/// 删除仓库
		/// </summary>
		/// <param name="info">要删除的仓库实体</param>
		/// <returns></returns>
		public async Task<Response<bool>> DelWarehouseManagerment(WarehouseDto info)
		{
			try
			{
				var result = info.MapTo<Warehouse>();
				var result1 = await _warehouseResponse.DeleteByIdAsync(result);
				return result1 > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
			}
			catch (Exception)
			{
				throw;
			}
		}

		/// <summary>
		/// 批量删除仓库
		/// </summary>
		/// <param name="info"></param>
		/// <returns></returns>
		/// <exception cref="NotImplementedException"></exception>
		public Task<Response<bool>> BatchDelWarehouseManagerment(WarehouseDto dto)
		{
			try
			{
				var sid = dto.DelId.Split(',');
				var list = new List<Warehouse>();
				foreach (var item in sid)
				{
					var warehouseList = _warehouseResponse.GetListQuery().Where(x => x.Id == int.Parse(item)).ToList();
					list.AddRange(warehouseList);
				}

				var result = _warehouseResponse.DeleteAsync(list);
				return result.Result > 0 ? Task.FromResult(Response<bool>.Ok(result.Result > 0)) : Task.FromResult(Response<bool>.Fail(result.Result > 0));
			}
			catch (Exception)
			{
				throw;
			}
		}
	}
}
